﻿using Sunny.UI;

namespace FZRD
{
    public partial class FUserEditForm : UIEditForm
    {
        public FUserEditForm()
        {
            InitializeComponent();

        }

        protected override bool CheckData()
        {
            return CheckEmpty(tbUserName, "请输入用户名称")
                   && CheckEmpty(tbPassword, "请输入用户密码")
                   && CheckEmpty(cbUserGroup, "请选择用户所属群组")
                   && CheckEmpty(cbPermission, "请选择用户权限");
        }
        public bool IsOK { get; private set; }

        public event EventHandler ButtonOkClick;
        public event EventHandler ButtonCancelClick;

        protected override void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }

            if (ButtonOkClick != null)
            {
                ButtonOkClick.Invoke(sender, e);
            }
            else
            {
                DialogResult = DialogResult.OK;
                IsOK = true;
                Close();
            }
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            if (ButtonCancelClick != null)
            {
                ButtonCancelClick.Invoke(sender, e);
            }
            else
            {
                DialogResult = DialogResult.Cancel;
                IsOK = false;
                Close();
            }
        }

        public UserEditEntity userEditEntity;

        public UserEditEntity UserEditEntity
        {
            get
            {
                if (userEditEntity == null)
                {
                    userEditEntity = new UserEditEntity();
                }
                userEditEntity.UserName = tbUserName.Text;
                userEditEntity.Password = tbPassword.Text;
                userEditEntity.GroupID = cbUserGroup.SelectedIndex;
                userEditEntity.Permission = cbPermission.SelectedIndex;

                return userEditEntity;
            }

            set
            {
                userEditEntity = value;
                tbUserName.Text = value.UserName;
                tbPassword.Text = value.Password;
                cbUserGroup.SelectedIndex = cbUserGroup.Items.IndexOf(value.GroupID);
                cbPermission.SelectedIndex = cbPermission.Items.IndexOf(value.Permission);
            }
        }
    }
}


public class UserEditEntity
{
    public int ID { get; set; }

    public string UserName { get; set; }

    public string Password { get; set; }

    public int GroupID { get; set; }

    public int Permission { get; set; }

    public long CreateTime { get; set; }

    public long LoginTime { get; set; }

    public long LogoutTime { get; set; }


    public override string ToString()
    {
        return UserName + ", " + Password + ", " + GroupID + ", " + Permission + ", " + CreateTime + ", " + LoginTime + ", " + LogoutTime;
    }
}